package com.example.demo.controller.admincontroller;

import com.example.demo.common.ErrorCode;
import com.example.demo.common.Result;
import com.example.demo.entity.Child;
import com.example.demo.service.ChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 儿童管理Controller
 */
@RestController
@RequestMapping("/admin")
public class ChildManageController {

    @Autowired
    private ChildService childService;

    /**
     * 获取用户的儿童列表
     */
    @GetMapping("/user/{userId}/children")
    public Result<List<Child>> getChildrenByUserId(@PathVariable Long userId) {
        List<Child> children = childService.getChildrenByUserId(userId);
        return Result.success(children);
    }

    /**
     * 获取儿童详情
     */
    @GetMapping("/children/{id}")
    public Result<Child> getChildById(@PathVariable Long id) {
        Child child = childService.getChildById(id);
        if (child == null) {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
        return Result.success(child);
    }

    /**
     * 添加儿童
     */
    @PostMapping("/children")
    public Result<Boolean> addChild(@RequestBody Child child) {
        if (childService.addChild(child)) {
            return Result.success(true);
        }
        return Result.error(ErrorCode.SYSTEM_ERROR);
    }

    /**
     * 更新儿童信息
     */
    @PutMapping("/children")
    public Result<Boolean> updateChild(@RequestBody Child child) {
        if (childService.updateChild(child)) {
            return Result.success(true);
        }
        return Result.error(ErrorCode.SYSTEM_ERROR);
    }

    /**
     * 删除儿童
     */
    @DeleteMapping("/children/{id}")
    public Result<Boolean> deleteChild(@PathVariable Long id) {
        if (childService.deleteChild(id)) {
            return Result.success(true);
        }
        return Result.error(ErrorCode.SYSTEM_ERROR);
    }
} 